﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LiteBlog.Common;
using LiteBlog.Models;
using LiteBlog.Components;
using LiteBlog.Common.Entities;
using LiteBlog.Common.Components;
using LiteBlog.Components.Contracts;
using System.Web.Script.Serialization;
using LiteBlog.Components.Enums;
using LiteBlog.Controllers.Filters;
using LiteBlog.Controllers.Contracts;

namespace LiteBlog.Controllers
{
    public class PageController : CompController<Page, string>, IPageController
    {
        private IPageComp _pageComponent = null;

        public PageController(IPageComp pageComp, ISettingsComp settingsComp, ICacheComp cacheComp)
            : base(pageComp, settingsComp, cacheComp)
        {
            _pageComponent = pageComp;
        }

        public override ActionResult Manage()
        {
            var collection = _component.Get();
            // strip the body to reduce the response size
            if (collection != null && collection.Count() > 0)
                foreach (var page in collection)
                    page.Body = string.Empty;

            return View("Manage", GetAdminLayout(), collection);
        }

        public override ActionResult Update(string olid, Page model)
        {
            return base.Update(olid, model);
        }

        [PageExceptionFilter]
        public ActionResult Index(string id)
        {
            var page = _pageComponent.GetPublished(id);
            if (page == null)
                return PageNotFound();
            return View("Index", GetSiteLayout(), page);
        }

        [HttpGet]
        [Authorize]
        [AjaxExceptionFilter]
        public ActionResult Preview(string id)
        {
            Page page = _component.Get(id);
            if (page == null)
                return PageNotFound();
            return View("Index", page);
        }

        [HttpGet]
        [Authorize]
        [PageExceptionFilter]
        public ActionResult Compose(string id)
        {
            Page page = null;
            if (string.IsNullOrEmpty(id))
            {
                page = new Page();
                page.FileId = Guid.NewGuid().ToString();
            }
            else
                page = _component.Get(id);

            var serializer = new JavaScriptSerializer();
            ViewBag.Json = serializer.Serialize(page);
            return View("Compose", GetAdminLayout(), null);
        }

        [HttpPost]
        [ValidateInput(false)]
        [AjaxExceptionFilter]
        [ValidateFilter]
        [Authorize]
        public ActionResult Save(Page page)
        {
            var result = _component.Create(page);
            if (result == MethodResult.CreateSuccess)
                result = MethodResult.SaveSuccess;
            var resultModel = GetResultModel(result, DateTimeHelper.Now.ToString("hh:mm:ss tt"));
            var resultModelWithData = new ResultModelWithData<string>(resultModel, page.FileId);
            return Json(resultModelWithData);
        }

        [HttpPost]
        [ValidateInput(false)]
        [Authorize]
        [AjaxExceptionFilter]
        public ActionResult Publish(Page page)
        {
            // Save the new page
            var result = _pageComponent.Publish(page);
            var resultModel = GetResultModel(result, page.Title);
            var resultModelWithData = new ResultModelWithData<string>(resultModel, page.FileId);
            return Json(resultModelWithData);
        }

        [HttpPost]
        [AjaxExceptionFilter]
        public ActionResult Unpublish(Page page)
        {
            var result = _pageComponent.Unpublish(page.FileId);
            var resultModel = GetResultModel(result, page.Title);
            return Json(resultModel);
        }

        protected override string[] GetMessageParams(Page model)
        {
            return new string[] { model.FileId };
        }

        public override void ResolveComponents()
        {

        }
    }
}
